var radio = document.querySelector('.radio');
var car = document.querySelector('.car');
var uls = radio.querySelectorAll('li')
var tt = document.querySelector('.tt');
var l = tt.querySelector('.l')
var r = tt.querySelector('.r')

var num = 0;
var focusWidth = 520;
// 鼠标移到轮播图
tt.addEventListener('mousemove',function() {
    l.style.display='block'
    r.style.display='block'
})
// 鼠标离开轮播图
tt.addEventListener('mouseout',function() {
    l.style.display='none'
    r.style.display='none'
})
// 右侧按钮
var flag = true;
    r.addEventListener('click', function() {
        if (flag) {
            flag = false;
            if(num == car.children.length-1) {
                car.style.left = '0';
                num = 0;
            }
            num++;
            animate(car, -num*focusWidth, function() {
                flag = true;
            });
            for(var i=0; i < uls.length; i++) {
                uls[i].className='';
            }
            if(num>=5) {
                uls[0].className='radio_c';
            }else {
                uls[num].className='radio_c';
            }
        }
    })
    // 左侧按钮
    l.addEventListener('click', function() {
        if (flag) {
            flag = false;
         if(num == 0) {
             num = car.children.length - 1;
             car.style.left = -num*focusWidth + 'px';
         }
         num--;
         animate(car, -num*focusWidth, function() {
             flag = true;
         });
         for(var i=0; i < uls.length; i++) {
            uls[i].className='';
        }
         if(num<0) {
            uls[4].className='radio_c';
        }else {
            uls[num].className='radio_c';
        }
        }
     })
var time =setInterval(function () {
    r.click();
},2000)

function animate(obj, target, callback) {
    clearInterval(obj.timer);
    obj.timer = setInterval(function() {
    var step = (target - obj.offsetLeft) / 10;
    step = step > 0 ? Math.ceil(step) : Math.floor(step);
    if(obj.offsetLeft == target) {
        clearInterval(obj.timer);
        callback && callback();
    }
    obj.style.left = obj.offsetLeft + step + 'px';
}, 15)
}